<template>
  <div>
    <zbTable ref="tableRef" :api-fun="get_list" @action="action">
      <template #toolbar>
        <a-space>
          <a-button type="primary" class="ele-btn-icon" @click="openEdit()">
            <template #icon>
              <plus-outlined />
            </template>
            <span>新建</span>
          </a-button>
          <a-button type="dashed" class="ele-btn-icon" @click="daochu()">
            <template #icon>
              <upload-outlined />
            </template>
            <span>导出</span>
          </a-button>
        </a-space>
      </template>
    </zbTable>
    <!-- 新增/编辑弹窗 -->
    <user-edit
      v-if="showEdit"
      v-model:visible="showEdit"
      :actionStatus="actionStatus"
      :editData="editData"
      @done="done"
    />
    <!-- 盘点报告弹窗 -->
    <report
      v-if="showReport"
      v-model:visible="showReport"
      :reportCode="reportCode"
    />
  </div>
</template>

<script setup>
  import { ref } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { useRouter } from 'vue-router';
  import { actionFn } from '@/utils/action.js';
  import { PlusOutlined, UploadOutlined } from '@ant-design/icons-vue';
  import { daochuFn } from '@/utils/daochu.js';
  import userEdit from './components/user-edit.vue';
  import report from './components/report.vue';
  import zbTable from '@/components/zbTable/jewelryPdIndex.vue';
  import { get_list, continue_check } from '@/api/jewelry/receipts_store_check';

  const { push } = useRouter();
  // 是否显示编辑弹窗
  const showEdit = ref(false);
  // 编辑数据
  const editData = ref({});
  // 编辑按钮数据
  const actionData = ref({});
  // 获取子组件
  const tableRef = ref(null);
  const actionStatus = ref('add');
  const showReport = ref(false);
  const reportCode = ref(null);

  /* 打开添加弹窗 */
  const openEdit = (row) => {
    editData.value.id = '';
    editData.value.code = '';
    showEdit.value = true;
    actionStatus.value = 'add';
    actionData.value = { url: '/jewelry/receipts_store_check/add' };
  };
  const action = (res) => {
    if (res.action_data.type == 'continue') {
      /* continue_check({ code: res.row_data.code })
        .then((r) => {
          toDetail(res.row_data.code);
        })
        .catch((e) => {
          message.error(e);
        }); */
      toDetail(res.row_data.code);
    } else if (res.action_data.type == 'report') {
      reportCode.value = res.row_data.code;
      showReport.value = true;
      console.log('reportCode', reportCode.value);
    }
  };
  const toDetail = (code) => {
    push({
      path: '/jewelry/receipts_store_check/detail',
      query: { code: code }
    });
  };
  const daochu = () => {
    let data = {
      url: 'jewelry/receipts_store_check/export',
      filename: '首饰盘点（门店）列表'
    };
    daochuFn(data);
  };
  const done = (form) => {
    actionFn(actionData.value, form, reloadTable);
  };
  const reloadTable = () => {
    showEdit.value = false;
    tableRef.value.reload();
  };
</script>
